package com.villaday.idf.custom.pop;

import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.Image;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.facebook.drawee.view.SimpleDraweeView;
import com.villaday.idf.R;
import com.villaday.idf.custom.LoadDialog;
import com.villaday.idf.model.Activity;
import com.villaday.idf.util.LogUtil;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.PopularUtil;
import com.villaday.idf.util.ToastUtil;

import razerdp.basepopup.BasePopupWindow;

public class SleepPop extends BasePopupWindow implements View.OnClickListener {

    private Context mContext;
    private final ImageView mIvSleep;


    public SleepPop(final Context context, String imgUri) {
        super(context);
        mContext = context;

        setBackground(0);
        setOutSideDismiss(true);
        setKeepSize(true);
        setPopupFadeEnable(false);
        setBackgroundColor(Color.parseColor("#B3000000"));
        setBlurBackgroundEnable(true);
//        findViewById(R.id.ll_sleep).startAnimation(PopularUtil.buildAnimal());

        findViewById(R.id.ll_wx).setOnClickListener(this);
        findViewById(R.id.ll_pyq).setOnClickListener(this);
        findViewById(R.id.ll_save).setOnClickListener(this);


        mIvSleep = findViewById(R.id.img);
        if (!TextUtils.isEmpty(imgUri)) {
            final RequestOptions options = new RequestOptions()
                    .error(R.drawable.shape_solid_white_20_wh)
                    .fitCenter()
                    .placeholder(R.drawable.shape_solid_white_20_wh);
            Glide.with(context).asBitmap().apply(options).load(imgUri).into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                @Override
                public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {

                    int width = resource.getWidth();
                    int height = resource.getHeight();
                    if (height > PhoneUtil.dp2px(context, 330)) {
                        mIvSleep.getLayoutParams().width = height / width * PhoneUtil.dp2px(context, 330);
                        mIvSleep.getLayoutParams().height = PhoneUtil.dp2px(context, 330);
                    } else {
                        mIvSleep.getLayoutParams().width = width;

                        mIvSleep.getLayoutParams().height = height;
                    }
                    mIvSleep.requestLayout();
                    mIvSleep.setImageBitmap(resource);

                }
            });
        }

        findViewById(R.id.iv_sleep_close).setOnClickListener(this);


    }

    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.popular_sleep);
    }


    @Override
    protected Animation onCreateDismissAnimation() {
        return PopularUtil.dismissAnimation();
    }


    @Override
    protected Animation onCreateShowAnimation() {
        return PopularUtil.ShowAnimation();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_wx:
            case R.id.ll_pyq:
            case R.id.ll_save:
                onBtnClickListener.onBtnClick(v);
            case R.id.iv_sleep_close:
                dismiss();
                break;
        }
    }

    OnBtnClickListener onBtnClickListener;

    public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
        this.onBtnClickListener = onBtnClickListener;
    }

    public interface OnBtnClickListener {
        void onBtnClick(View view);
    }


}
